﻿using ICSharpCode.TextEditor.Document;
using System.Collections.Generic;

namespace controls.TextEditor
{
    internal class FoldingCode : IFoldingStrategy
    {
        /// <summary>
        /// @@ 折行
        /// </summary>
        /// <param name="document"></param>
        /// <param name="fileName"></param>
        /// <param name="parseInformation"></param>
        /// <returns></returns>
        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            List<FoldMarker> list = new List<FoldMarker>();

            var startLines = new Stack<int>();

            for (int i = 0; i < document.TotalNumberOfLines; i++)
            {
                // Get the text of current line.
                string text = document.GetText(document.GetLineSegment(i));

                if (text.Trim().Length >= 2 && text.Trim().Substring(0, 2) == "@@") // Look for method starts
                {
                    if (flag == false)
                    {
                        if (startLines.Count > 0)
                        {
                            int start = startLines.Pop();
                            // 判断相邻两行是否都有标签
                            if (start == i - 1)
                            {
                                startLines.Push(start);
                            }
                            else
                            {
                                list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i - 1, 57, FoldType.TypeBody, "...}", true));

                                count++;
                            }
                        }

                        startLines.Push(i);
                    }
                    else
                    {
                        flag = false;
                        startLines.Push(i);
                    }
                }
            }

            return list;
        }

        private int count = 0;

        //是否是第一个标签
        private bool flag = true;
    }
}